





<!DOCTYPE html>
<html class="writer-html5" lang="zh-CN" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Error Handling Guide &mdash; tvm 0.8.dev1982 文档</title>
  

  
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/gallery.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tlcpack_theme.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../_static/tvm-logo-square.png"/>
  

  
  
  
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <script type="text/javascript" src="../_static/js/tlcpack_theme.js"></script>
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="Submit a Pull Request" href="pull_request.html" />
    <link rel="prev" title="代码指南和提示" href="code_guide.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    
<header class="header">
    <div class="innercontainer">
      <div class="headerInner d-flex justify-content-between align-items-center">
          <div class="headerLogo">
               <a href="https://tvm.apache.org/"><img src=https://tvm.apache.org/assets/images/logo.svg alt="logo"></a>
          </div>

          <div id="headMenu" class="headerNav">
            <button type="button" id="closeHeadMenu" class="navCloseBtn"><img src="../_static/img/close-icon.svg" alt="Close"></button>
             <ul class="nav">
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/community>Community</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/download>Download</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/vta>VTA</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/blog>Blog</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/docs>Docs</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvmconf.org>Conference</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://github.com/apache/tvm/>Github</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvmchinese.github.io/declaration_zh_CN.html>About-Translators</a>
                </li>
             </ul>
               <div class="responsivetlcdropdown">
                 <button type="button" class="btn-link">
                   ASF
                 </button>
                 <ul>
                     <li>
                       <a href=https://apache.org/>Apache Homepage</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/licenses/>License</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/sponsorship.html>Sponsorship</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/security/>Security</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/thanks.html>Thanks</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/events/current-event>Events</a>
                     </li>
                     <li>
                       <a href=https://www.zhihu.com/column/c_1429578595417563136>Zhihu</a>
                     </li>
                 </ul>
               </div>
          </div>
            <div class="responsiveMenuIcon">
              <button type="button" id="menuBtn" class="btn-menu"><img src="../_static/img/menu-icon.svg" alt="Menu Icon"></button>
            </div>

            <div class="tlcDropdown">
              <div class="dropdown">
                <button type="button" class="btn-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  ASF
                </button>
                <div class="dropdown-menu dropdown-menu-right">
                  <ul>
                     <li>
                       <a href=https://apache.org/>Apache Homepage</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/licenses/>License</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/sponsorship.html>Sponsorship</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/security/>Security</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/thanks.html>Thanks</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/events/current-event>Events</a>
                     </li>
                     <li>
                       <a href=https://www.zhihu.com/column/c_1429578595417563136>Zhihu</a>
                     </li>
                  </ul>
                </div>
              </div>
          </div>
       </div>
    </div>
 </header>
 
    <nav data-toggle="wy-nav-shift" class="wy-nav-side fixed">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html">
          

          
            
            <img src="../_static/tvm-logo-small.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
                <div class="version">
                  0.8.dev1982
                </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption" role="heading"><span class="caption-text">如何开始</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../install/index.html">安装 TVM</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">贡献者指南</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="community.html">TVM社区指南</a></li>
<li class="toctree-l2"><a class="reference internal" href="code_review.html">执行代码审查</a></li>
<li class="toctree-l2"><a class="reference internal" href="committer_guide.html">Committer Guide</a></li>
<li class="toctree-l2"><a class="reference internal" href="document.html">Write Documentation for TVM</a></li>
<li class="toctree-l2"><a class="reference internal" href="code_guide.html">代码指南和提示</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Error Handling Guide</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#raise-a-specific-error-in-c">Raise a Specific Error in C++</a></li>
<li class="toctree-l3"><a class="reference internal" href="#how-to-choose-an-error-type">How to choose an Error Type</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="pull_request.html">Submit a Pull Request</a></li>
<li class="toctree-l2"><a class="reference internal" href="git_howto.html">Git 使用技巧</a></li>
<li class="toctree-l2"><a class="reference internal" href="release_process.html">Apache TVM Release Process</a></li>
</ul>
</li>
</ul>
<p class="caption" role="heading"><span class="caption-text">用户引导</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">User Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../how_to/index.html">How To Guides</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">开发者引导</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../dev/tutorial/index.html">Developer Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dev/how_to/how_to.html">开发者指南</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">架构指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../arch/index.html">Design and Architecture</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">主题引导</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../topic/microtvm/index.html">microTVM：裸机使用TVM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topic/vta/index.html">VTA: Versatile Tensor Accelerator</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">参考指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../reference/langref/index.html">语言参考</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/api/python/index.html">Python API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/api/links.html">Other APIs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../genindex.html">索引</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
      
      <nav class="wy-nav-top" aria-label="top navigation" data-toggle="wy-nav-top">
        
            <div class="togglemenu">

            </div>
            <div class="nav-content">
              <!-- tvm -->
              Table of content
            </div>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        

          




















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> <span class="br-arrow">></span></li>
        
          <li><a href="index.html">贡献者指南</a> <span class="br-arrow">></span></li>
        
      <li>Error Handling Guide</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../_sources/contribute/error_handling.rst.txt" rel="nofollow"> <img src="../_static//img/source.svg" alt="viewsource"/></a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="error-handling-guide">
<span id="id1"></span><h1>Error Handling Guide<a class="headerlink" href="#error-handling-guide" title="永久链接至标题">¶</a></h1>
<p>TVM contains structured error classes to indicate specific types of error.
Please raise a specific error type when possible, so that users can
write code to handle a specific error category if necessary.
You can directly raise the specific error object in python.
In other languages like c++, you simply add <code class="docutils literal notranslate"><span class="pre">&lt;ErrorType&gt;:</span></code> prefix to
the error message(see below).</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>Please refer to <a class="reference internal" href="../reference/api/python/error.html#module-tvm.error" title="tvm.error"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tvm.error</span></code></a> for the list of errors.</p>
</div>
<div class="section" id="raise-a-specific-error-in-c">
<h2>Raise a Specific Error in C++<a class="headerlink" href="#raise-a-specific-error-in-c" title="永久链接至标题">¶</a></h2>
<p>You can add <code class="docutils literal notranslate"><span class="pre">&lt;ErrorType&gt;:</span></code> prefix to your error message to
raise an error of the corresponding type.
Note that you do not have to add a new type
<a class="reference internal" href="../reference/api/python/error.html#tvm.error.TVMError" title="tvm.error.TVMError"><code class="xref py py-class docutils literal notranslate"><span class="pre">tvm.error.TVMError</span></code></a> will be raised by default when
there is no error type prefix in the message.
This mechanism works for both <code class="docutils literal notranslate"><span class="pre">LOG(FATAL)</span></code> and <code class="docutils literal notranslate"><span class="pre">ICHECK</span></code> macros.
The following code gives an example on how to do so.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="c1">// src/api_test.cc</span>
<span class="kt">void</span> <span class="nf">ErrorTest</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span>
  <span class="n">ICHECK_EQ</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;ValueError: expect x and y to be equal.&quot;</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">LOG</span><span class="p">(</span><span class="n">FATAL</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;InternalError: cannot reach here&quot;</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The above function is registered as PackedFunc into the python frontend,
under the name <code class="docutils literal notranslate"><span class="pre">tvm._api_internal._ErrorTest</span></code>.
Here is what will happen if we call the registered function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">tvm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tvm</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">ErrorTest</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  File <span class="nb">&quot;/path/to/tvm/python/tvm/_ffi/_ctypes/function.py&quot;</span>, line <span class="m">190</span>, in <span class="n">__call__</span>
    <span class="k">raise</span> <span class="n">get_last_ffi_error</span><span class="p">()</span>
<span class="gr">ValueError</span>: <span class="n">Traceback (most recent call last):</span>
<span class="go">  [bt] (3) /path/to/tvm/build/libtvm.so(TVMFuncCall+0x48) [0x7fab500b8ca8]</span>
<span class="go">  [bt] (2) /path/to/tvm/build/libtvm.so(+0x1c4126) [0x7fab4f7f5126]</span>
<span class="go">  [bt] (1) /path/to/tvm/build/libtvm.so(+0x1ba2f8) [0x7fab4f7eb2f8]</span>
<span class="go">  [bt] (0) /path/to/tvm/build/libtvm.so(+0x177d12) [0x7fab4f7a8d12]</span>
  File <span class="nb">&quot;/path/to/tvm/src/api/api_test.cc&quot;</span>, line <span class="m">80</span>
<span class="gr">ValueError</span>: <span class="n">Check failed: x == y (0 vs. 1) : expect x and y to be equal.</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tvm</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">ErrorTest</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  File <span class="nb">&quot;/path/to/tvm/python/tvm/_ffi/_ctypes/function.py&quot;</span>, line <span class="m">190</span>, in <span class="n">__call__</span>
    <span class="k">raise</span> <span class="n">get_last_ffi_error</span><span class="p">()</span>
<span class="gr">tvm.error.InternalError</span>: <span class="n">Traceback (most recent call last):</span>
<span class="go">  [bt] (3) /path/to/tvm/build/libtvm.so(TVMFuncCall+0x48) [0x7fab500b8ca8]</span>
<span class="go">  [bt] (2) /path/to/tvm/build/libtvm.so(+0x1c4126) [0x7fab4f7f5126]</span>
<span class="go">  [bt] (1) /path/to/tvm/build/libtvm.so(+0x1ba35c) [0x7fab4f7eb35c]</span>
<span class="go">  [bt] (0) /path/to/tvm/build/libtvm.so(+0x177d12) [0x7fab4f7a8d12]</span>
  File <span class="nb">&quot;/path/to/tvm/src/api/api_test.cc&quot;</span>, line <span class="m">83</span>
<span class="gr">InternalError</span>: <span class="n">cannot reach here</span>
<span class="go">TVM hint: You hit an internal error. Please open a thread on https://discuss.tvm.ai/ to report it.</span>
</pre></div>
</div>
<p>As you can see in the above example, TVM’s ffi system combines
both the python and c++’s stacktrace into a single message, and generate the
corresponding error class automatically.</p>
</div>
<div class="section" id="how-to-choose-an-error-type">
<h2>How to choose an Error Type<a class="headerlink" href="#how-to-choose-an-error-type" title="永久链接至标题">¶</a></h2>
<p>You can go through the error types are listed below, try to use common
sense and also refer to the choices in the existing code.
We try to keep a reasonable amount of error types.
If you feel there is a need to add a new error type, do the following steps:</p>
<ul class="simple">
<li><p>Send a RFC proposal with a description and usage examples in the current codebase.</p></li>
<li><p>Add the new error type to <a class="reference internal" href="../reference/api/python/error.html#module-tvm.error" title="tvm.error"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tvm.error</span></code></a> with clear documents.</p></li>
<li><p>Update the list in this file to include the new error type.</p></li>
<li><p>Change the code to use the new error type.</p></li>
</ul>
<p>We also recommend to use less abstraction when creating the short error messages.
The code is more readable in this way, and also opens path to craft specific
error messages when necessary.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">preferred</span><span class="p">():</span>
    <span class="c1"># Very clear about what is being raised and what is the error message.</span>
    <span class="k">raise</span> <span class="n">OpNotImplemented</span><span class="p">(</span><span class="s2">&quot;Operator relu is not implemented in the MXNet frontend&quot;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">_op_not_implemented</span><span class="p">(</span><span class="n">op_name</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">OpNotImplemented</span><span class="p">(</span><span class="s2">&quot;Operator {} is not implemented.&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">op_name</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">not_preferred</span><span class="p">():</span>
    <span class="c1"># Introduces another level of indirection.</span>
    <span class="k">raise</span> <span class="n">_op_not_implemented</span><span class="p">(</span><span class="s2">&quot;relu&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>If we need to introduce a wrapper function that constructs multi-line error messages,
please put wrapper in the same file so other developers can look up the implementation easily.</p>
</div>
</div>


           </div>
           
          </div>
          

<footer>

    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="pull_request.html" class="btn btn-neutral float-right" title="Submit a Pull Request" accesskey="n" rel="next">下一个 <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="code_guide.html" class="btn btn-neutral float-left" title="代码指南和提示" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> 上一个</a>
      
    </div>

<div id="button" class="backtop"><img src="../_static//img/right.svg" alt="backtop"/> </div>
<section class="footerSec">
    <div class="footerHeader">
      <ul class="d-flex align-md-items-center justify-content-between flex-column flex-md-row">
        <li class="copywrite d-flex align-items-center">
          <h5 id="copy-right-info">© 2020 Apache Software Foundation | All right reserved</h5>
        </li>
      </ul>

    </div>

    <ul>
      <li class="footernote">Copyright © 2020 The Apache Software Foundation. Apache TVM, Apache, the Apache feather, and the Apache TVM project logo are either trademarks or registered trademarks of the Apache Software Foundation.</li>
    </ul>

</section>
</footer>
        </div>
      </div>

    </section>

  </div>
  

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

  </body>
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-75982049-2', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>